﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Security;
using System.Text;
using BLL;

namespace WebUI
{
    public partial class Default : System.Web.UI.MasterPage
    {
        void Page_Load(object sender, EventArgs e)
        {
            UserController usrController = new UserController();
            RequestController reqController = new RequestController();
            TeamController tmController = new TeamController();

            int idUsuario = 0;

            try
            {
                idUsuario = usrController.GetUserID(Page.User.Identity.Name);
            }
            catch (Exception ex)
            {
                Response.Redirect("unregistredUser.aspx");
            }
            finally { }

            string Nome = usrController.GetUserName(idUsuario);
            lblUsuario.Text = Nome;
            char Status = 'A';
            int idTeam = usrController.GetUserTeamID(idUsuario);

            switch (idTeam)
            {
                case 1:
                    Status = 'D';
                    break;
                case 2:
                    Status = 'D';
                    break;
                case 3:
                    Status = 'D';
                    break;
                case 9:
                    Status = 'A';
                    break;
                default:
                    break;
            }

            int count = reqController.CountRequests(idUsuario, Status);

            if (idTeam == 1 | idTeam == 2)
            {
                count = count + reqController.CountRequests(idUsuario, 'A');
            }

            string plural = "";

            if (count == 1)
            {
                plural = " solicitação.";
            }
            else
            {
                plural = " solicitações.";
            }

            lblTime.Text = tmController.GetTeamName(idTeam);
            lblSolicitacoes.Text = "Você tem " + count.ToString() + plural;
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            UserController usrController = new UserController();
            RequestController reqController = new RequestController();
            TeamController tmController = new TeamController();

            int idUsuario = usrController.GetUserID(Page.User.Identity.Name);

            string Nome = usrController.GetUserName(idUsuario);
            char Status = 'A';
            int idTeam = usrController.GetUserTeamID(idUsuario);

            switch (idTeam)
            {
                case 1:
                    Status = 'D';
                    break;
                case 2:
                    Status = 'D';
                    break;
                case 3:
                    Status = 'D';
                    break;
                case 9:
                    Status = 'A';
                    break;
                default:
                    break;
            }

            int count = reqController.CountRequests(idUsuario, Status);

            if (idTeam == 1 | idTeam == 2)
            {
                count = count + reqController.CountRequests(idUsuario, 'A');
            }

            string plural = "";

            if (count == 1)
            {
                plural = " solicitação.";
            }
            else
            {
                plural = " solicitações.";
            }

            lblTime.Text = tmController.GetTeamName(idTeam);
            lblSolicitacoes.Text = "Você tem " + count.ToString() + plural;
        }
    }
}
